package cn.yunhe.action;

import cn.yunhe.biz.EmpBiz;
import cn.yunhe.entity.Dept;
import cn.yunhe.entity.Emp;
import cn.yunhe.entity.Page;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Scope(value = "prototype")
@ParentPackage("json-default")
public class EmpAction extends BasicAction implements ModelDriven  {
    @Resource
    private EmpBiz empBiz;

  /*private List<Emp> empList;

  private Emp emp ;*/

  private Page page;

  @Action(value = "allemp",results = {
          @Result(name="success",type = "json",params = {"root","page"})
  })
   public String AllEmp(){
     /*List<Emp>   empList =empBiz.getAllEmp();*/
      Integer counts = empBiz.getCount();
      List<Emp> empList = empBiz.searchEmp(page);
      page.setCounts(counts);
      page.setEmpList(empList);
       return SUCCESS;
   }

   @Action(value = "searchempbyid",results = {
           @Result(name = "success",location = "/update.jsp")
   })
   public String searchEmpById(){
      Emp emp = page.getEmp();
      List<Dept> deptList = empBiz.getAllDept();
     emp =  empBiz.getEmpById(emp.getEmpid());
     getMapRequest().put("emp",emp);
     getMapRequest().put("deptList",deptList);
     return SUCCESS;
   }

   @Action(value = "updateemp",results = {
           @Result(name = "success",location = "/main.jsp")
   })
   public String updateEmp(){
       Emp emp = page.getEmp();
      empBiz.updateEmp(emp);
      return SUCCESS;
   }

   @Action(value = "deleteemp",results = {
           @Result(name = "success",location = "/main.jsp")
   })
   public String deleteEmp(){
      Emp emp = page.getEmp();
      empBiz.delEmp(emp);
      return SUCCESS;
   }


   /* public List<Emp> getEmpList() {
        return empList;
    }

    public void setEmpList(List<Emp> empList) {
        this.empList = empList;
    }

    public Emp getEmp() {
        return emp;
    }

    public void setEmp(Emp emp) {
        this.emp = emp;
    }*/

    public Page getPage() {
        return page;
    }

    public void setPage(Page page) {
        this.page = page;
    }

    public void setEmpBiz(EmpBiz empBiz) {
        this.empBiz = empBiz;
    }

    @Override
    public Object getModel() {
     if(page ==null){
         page = new Page();
     }
     return page;
  }
}
